//
//  E7StageData.cpp
//  EndlessBattle
//
//  Created by cc on 12-4-21.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7StageData.h"

CE7StageData* s_pStageData = NULL ;

CE7StageData::CE7StageData()
{
    m_bIsSaved = false ;
    m_nMapCount = 0 ;

    m_bIsSaved = CCUserDefault::sharedUserDefault()->getBoolForKey("e7click_saved_flag") ;
    printf("CE7StageData::CE7StageData m_bIsSaved: [%d]\n", m_bIsSaved) ;

    m_nMapCount = CCUserDefault::sharedUserDefault()->getIntegerForKey("endless_battle_map_count") ;
    printf("CE7StageData::CE7StageData m_nMapCount: [%d]\n", m_nMapCount) ;

//    for (int i = 0; i < STAGE_COUNT; ++i)
//    {
//        SetStageLevel(i, 3) ;
//    }
}

CE7StageData::~CE7StageData()
{
}

CE7StageData* CE7StageData::GetStageData()
{
    if (NULL == s_pStageData)
    {
        s_pStageData = new CE7StageData() ;
    }

    return s_pStageData ;
}

void CE7StageData::SetStageLevel(int nID, int point)
{
    if (point <= GetStageLevel(nID))
    {
        return ;
    }

    char strIndex[128] ;
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "stage_%d_mappoint", nID) ;
    CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, point) ;

    SetCompleteCount(GetCompleteCount()+1) ;
    CCUserDefault::sharedUserDefault()->flush() ;
}

int CE7StageData::GetStageLevel(int nID)
{
    char strIndex[128] ;
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "stage_%d_mappoint", nID) ;
    int res = CCUserDefault::sharedUserDefault()->getIntegerForKey(strIndex) ;
    return res ;
}

//void CE7StageData::AddCompleteCount()
//{
//    char strIndex[128] ;
//    ::memset(strIndex, 0, 128) ;
//    sprintf(strIndex, "complete_count") ;
//    CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, GetCompleteCount()+1) ;
//}

void CE7StageData::SetCompleteCount(int count)
{
    char strIndex[128] ;
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "complete_count") ;
    CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, count) ;
    CCUserDefault::sharedUserDefault()->flush() ;
    printf("set count: [%d]\n", count) ;
}

int CE7StageData::GetCompleteCount()
{
    char strIndex[128] ;
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "complete_count") ;
    int res = CCUserDefault::sharedUserDefault()->getIntegerForKey(strIndex) ;
    return res ;
}

void CE7StageData::SaveMapData(int nID, int mapid, char* mapname, int mapsize, int* pData)
{
    printf("Enter CE7StageData::SaveMapData : [%d][%d][%s][%d]\n", nID, mapid, mapname, mapsize) ;
    struct timeval ts, te ;
    gettimeofday(&ts, NULL) ;

    char strIndex[128] ;

    // save game complete percent.
    SetCompleteCount(0) ;

    // save map info. index = mapid.
    // save map id.
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "stage_%d_mapid", nID) ;
    printf("save data - 1: [%s][%d]\n", strIndex, mapid) ;
    CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, mapid) ;
    // save map name.
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "stage_%d_mapname", nID) ;
    printf("save data - 2: [%s][%s]\n", strIndex, mapname) ;
    CCUserDefault::sharedUserDefault()->setStringForKey(strIndex, mapname) ;
    // save map size.
    ::memset(strIndex, 0, 128) ;
    sprintf(strIndex, "stage_%d_mapsize", nID) ;
    printf("save data - 3: [%s][%d]\n", strIndex, mapsize) ;
    CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, mapsize) ;
    // save map point.
    SetStageLevel(nID, 0) ;
    //////////////////// save map head end.

    for (int i = 0; i < mapsize; ++i)
    {
        ::memset(strIndex, 0, 128) ;
        ::sprintf(strIndex, "stage_%d_%d_%d", nID, mapid, i) ;
        CCUserDefault::sharedUserDefault()->setIntegerForKey(strIndex, pData[i]) ;
//        printf("save data map info: [%d]\n", pData[i]) ;
    }

    // update map save flag.
    m_bIsSaved = true ;
    CCUserDefault::sharedUserDefault()->setBoolForKey("e7click_saved_flag", m_bIsSaved) ;
    // update map count.
    CCUserDefault::sharedUserDefault()->setIntegerForKey("endless_battle_map_count", ++m_nMapCount) ;

    gettimeofday(&te, NULL) ;
    printf("use time : [%lu] micro second\n", (te.tv_sec-ts.tv_sec)*1000000 + (te.tv_usec-ts.tv_usec)) ;

    CCUserDefault::sharedUserDefault()->flush() ;
    printf("Exit CE7StageData::SaveMapData now map count [%d]\n", m_nMapCount) ;
}

void CE7StageData::LoadMapData(int nID, int& mapid, char* mapname, int& mapsize, int** pData)
{
    printf("Enter CE7StageData::LoadMapData : [%d] [%d]\n", nID, m_nMapCount) ;
    struct timeval ts, te ;
    gettimeofday(&ts, NULL) ;
    if (!m_bIsSaved)
    {
        printf("CE7StageData::LoadMapData no saved map\n") ;
        return ;
    }

    char strIndex[128] ;
    // load map id.
    ::memset(strIndex, 0, 128) ;
    ::sprintf(strIndex, "stage_%d_mapid", nID) ;
    mapid = CCUserDefault::sharedUserDefault()->getIntegerForKey(strIndex) ;
    // load map name.
    ::memset(strIndex, 0, 128) ;
    ::sprintf(strIndex, "stage_%d_mapname", nID) ;
    mapname = (char*)(CCUserDefault::sharedUserDefault()->getStringForKey(strIndex).c_str()) ;
    // load map size.
    ::memset(strIndex, 0, 128) ;
    ::sprintf(strIndex, "stage_%d_mapsize", nID) ;
    mapsize = CCUserDefault::sharedUserDefault()->getIntegerForKey(strIndex) ;

    *pData = new int[mapsize] ;
    for (int i = 0; i < mapsize; ++i)
    {
        ::memset(strIndex, 0, 128) ;
        ::sprintf(strIndex, "stage_%d_%d_%d", nID, mapid, i) ;
        (*pData)[i] = CCUserDefault::sharedUserDefault()->getIntegerForKey(strIndex) ;
//        printf("load map info: [%d]\n", (*pData)[i]) ;
    }
    gettimeofday(&te, NULL) ;
    printf("use time : [%lu] micro second\n", (te.tv_sec-ts.tv_sec)*1000000 + (te.tv_usec-ts.tv_usec)) ;

    CCUserDefault::sharedUserDefault()->flush() ;
    printf("Exit CE7StageData::LoadMapData [%d][%d][%s][%d]\n", nID, mapid, mapname, mapsize) ;
}






















